<template>
	<view class="product">
		<view class="title">
			<view @tap="titleClick(item)" class="items" :class="currentIndex == index ? 'active' : ''" v-for="(item, index) in titleList"
			 :key="item.id">
				{{item.name}}
			</view>
		</view>
		<view class="product-main">
			<view @tap="itemClick(item)" class="item" v-for="item in productList" :key="item.id">
				<view class="imgs">
					<image :src="item.cover_img" mode=""></image>
				</view>
				<view class="bottom">
					<view class="gname">{{item.gname}}</view>
					<view class="bottom-price">
						<view class="price">{{item.price_now | priceFilter}}</view>
						<view class="city">{{item.origin_city}}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- <uni-popup ref="popup" type="bottom">底部弹出 Popup</uni-popup> -->
		<uni-popup ref="popup" type="message">
		    <uni-popup-message type="success" message="成功消息" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
import uniPopup from '@/components/uni-popup/uni-popup.vue'
import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	import {
		getProductList
	} from '@/network/product/index.js'
	export default {
		data() {
			return {
				kindId: '',
				titleList: [{
						name: '新品',
						id: 1
					},
					{
						name: '销量',
						id: 2
					},
					{
						name: '价格',
						id: 3
					},
					{
						name: '刷选',
						id: 4
					},
				],
				currentIndex: 0,
				productList: []
			};
		},
		components: {
			uniPopup,
			uniPopupMessage,
			uniPopupDialog
		},
		onLoad(options) {
			console.log(options.kindId, '1')
			this.kindId = options.kindId;
			this.getProductLists()
		},
		methods: {
			async getProductLists() {
				const res = await getProductList({
					kindId: this.kindId,
					start: 1,
					limit: 20
				})
				console.log(res)
				this.productList = res.data.data.list
			},
			itemClick(item) {
				console.log(item)
				let index = this.productList.findIndex(items => items.id == item.id)
				uni.navigateTo({
					url: '/pages/product/productDetail/productDetail?productId=' + this.productList[index].id + `&img=${item.cover_img}`
				})
			},
			titleClick(item) {
				let index = this.titleList.findIndex(items => items.id == item.id)
				this.currentIndex = index;
				if (this.currentIndex == 2) {
					this.$refs.popup.open()
					this.productList = this.productList.sort((a, b) => {
						return a.price_now - b.price_now
					})
				} else if (this.currentIndex == 3) {
					this.productList = this.productList.sort((a, b) => {
						return b.price_now - a.price_now
					})
				} else {
					this.getProductLists()
				}
			}
		},
		filters: {
			priceFilter(val) {
				return "¥" + (val / 100).toFixed(2)
			}
		},


	}
</script>

<style lang="scss" scoped>
	.product {
		background-color: #F1ECE7;
	}

	.title {
		// position: fixed;
		// left: 0;
		// top: 60rpx;
		// width: 100%;
		// z-index: 2;
		display: flex;
		justify-content: space-around;
		align-items: center;
		background-color: #354E44;
		color: #fff;
		height: 76rpx;
		padding-top: 14rpx;

		// font-size: 28rpx;
		.active {
			color: #DD524D;
		}
	}

	.product-main {
		// margin: ;
		padding: 54rpx 40rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.item {
			// 158 120
			// width: 48%;
			width: 316rpx;
			margin-bottom: 40rpx;
			background-color: #fff;

			.imgs {
				width: 316rpx;
				height: 240rpx;
				margin-bottom: 40rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.bottom {
				padding: 0rpx 20rpx 20rpx;

				.gname {
					font-size: 28rpx;
					font-weight: 700;
					margin-bottom: 10rpx;
				}

				.bottom-price {
					display: flex;
					justify-content: space-between;
					font-size: 24rpx;

					.price {
						font-weight: 700;
					}
				}
			}
		}
	}
</style>
